package com.example.newland.application;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.nle.mylibrary.claimer.rfid.SingleEpcListener;
import com.nle.mylibrary.device.RFID;

public class OneActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView one_iv_setting;
    private Intent intent;
    private TextView one_tv_epc;
    private ImageView one_iv_image;
    private RFID rfid;


    private  boolean isNew = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one);

        one_iv_setting = findViewById(R.id.one_iv_setting);
        one_tv_epc = findViewById(R.id.one_tv_epc);
        one_iv_image = findViewById(R.id.one_iv_image);

        one_iv_setting.setOnClickListener(this);

        Log.i("one", "主线程读取中。。。"+ Thread.currentThread().getName());

        isNew = true;
        startRfid();
    }

    private boolean b = true;

    public void startRfid(){

        b = true;

        new Thread(){
            @Override
            public void run() {

                while (true){
                    if(b){


                        Log.i("one", "读取中。。。"+ Thread.currentThread().getName());


                        rfid = EpcInit.epc1();
                        try {
                            rfid.readSingleEpc(new SingleEpcListener() {
                                @Override
                                public void onVal(String s) {
                                    one_tv_epc.setText(s);
                                    SharedPreferences config = getSharedPreferences("config", MODE_PRIVATE);
                                    String epc = config.getString(s, "");
                                    if(epc != null && !"".equals(epc)){
                                        Log.i("one", "epc读取中。。。"+ Thread.currentThread().getName());

//                                        Message m  = new Message();
//                                        m.what = 1;
                                        b = false;
                                        handler.sendEmptyMessageDelayed(1,3000);

                                        Log.i("one","跳转第二个界面");
                                    }
                                }

                                @Override
                                public void onFail(Exception e) {

                                }
                            });
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        try {

                            Thread.sleep(5000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }else{
                        //跳出是循环
                        break;
                    }
                }

            }
        }.start();
    }

    @Override
    protected void onStart() {
        super.onStart();

        if(!isNew){
            startRfid();
        }else{
            isNew = false;
        }
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.one_iv_setting:
                intent = new Intent(OneActivity.this,MainActivity.class);
                startActivity(intent);
                b = false;
                break;
        }
    }

   //3
    Handler handler = new Handler(Looper.myLooper()){
       @Override
       public void handleMessage(Message msg) {

            switch (msg.what){

                case 1 :
                    //线程关闭
                    b = false;

                    // 闸机播放动画

                    //跳转界面
                    Intent intent = new Intent(OneActivity.this,TwoActivity.class);
                    startActivity(intent);
                    break;
            }
       }
   };
}
